/**
 * 
 */
package src.ejemploPrint;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.JobSheets;

/**
 * @author javaOne
 *
 */
public class TicketPrintPage implements Printable {

	/**
	 * 
	 */
    private File ticket;

    
	public TicketPrintPage() {
		// TODO Auto-generated constructor stub
	}
	
    public TicketPrintPage(File f) {
        ticket = f;
    }
    
	/* (non-Javadoc)
	 * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
	 */
	public int print(Graphics arg0, PageFormat arg1, int arg2)
			throws PrinterException {
		// TODO Auto-generated method stub
        int interline = 12;
        Graphics2D g2 = (Graphics2D) arg0;
        g2.setFont(new Font("CourierThai", Font.PLAIN, 10));
        int x =  (int) arg1.getImageableX();
        int y = (int) arg1.getImageableY();

        try {
            FileReader fr = new FileReader(ticket);
            BufferedReader br = new BufferedReader(fr);

            String s;
            while ((s = br.readLine()) != null) {
                y += interline;
                g2.drawString(s, x, y);
            }
        } catch (IOException e) {
            throw new PrinterException("File to print does not exist (" + ticket.getAbsolutePath() +") !");
        }
        return Printable.PAGE_EXISTS;
    
	}

	public void printTicketFile(File ticket, int orientation) throws PrinterException {
	    if (!ticket.exists()) {
	        throw new PrinterException("Ticket to print does not exist (" + ticket.getAbsolutePath() + ") !");
	    }
	    PrinterJob pjob = PrinterJob.getPrinterJob();
	    // get printer using PrintServiceLookup.lookupPrintServices(null, null) and looking at the name
	    //pjob.setPrintService(getPrintService());
	    // job title
	    pjob.setJobName(ticket.getName());

	    // page fomat
	    PageFormat pf = pjob.defaultPage();
	    // landscape or portrait
	    pf.setOrientation(orientation);
	    // Paper properties
	    Paper a4Paper = new Paper();
	    double paperWidth  =  8.26;
	    double paperHeight = 11.69;
	    double margin = 16;
	    a4Paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
	    a4Paper.setImageableArea(
	                margin,
	                //0,
	                margin,
	                //0,
	                a4Paper.getWidth()- 2 * margin,
	                //a4Paper.getWidth(),
	                a4Paper.getHeight()- 2 * margin
	                //a4Paper.getHeight()
	                ); // no margin = no scaling
	    pf.setPaper(a4Paper);
	    // Custom class that defines how to layout file text
	    TicketPrintPage pages = new TicketPrintPage(ticket);
	    // adding the page to a book
	    Book book = new Book();
	    book.append(pages, pf);
	    // Adding the book to a printjob
	    pjob.setPageable(book);
	    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
	    	// No jobsheet (banner page, the page with user name, job name, date and whatnot)
	    pras.add(JobSheets.NONE);
	    // Printing
	    pjob.print(pras);
	}
	
}
